/*
    原型链继承
*/
function superType() {
}

superType.prototype.setname = function(){
    this.setname = {nm: '父类的名字'}
}

function subType() {
}

subType.prototype = new superType()
let sub1 = new subType()
sub1.setname.nm = 'sub1的名字'
let sub2 =  new subType()
console.log(sub2.setname.nm) // 输出：sub1的名字，引用类型已经被共享了

/*=======================================================================*/
/*
    借用构造函数继承
*/

function superType (age) {
    this.setname = {nm:'父类的名字'}
    this.age = age
}
superType.prototype.food = function(){
    return '大盘鸡'
}
function subType(age) {
    superType.call(this,age)
}

let sub1 = new subType(25)
sub1.setname.nm = 'sub1的名字'
let sub2 = new subType(30)
console.log(sub1) // 输出：{ setname: { nm: 'sub1的名字' }, age: 25 }
console.log(sub2) // 输出：{ setname: { nm: '父类的名字' }, age: 30 }
console.log(sub2.food) // 输出：undefined

/*=======================================================================*/

/* 组合继承 */
function superType(age) {
    this.age = age
    this.setname = {nm: '父类的名字'}
}
superType.prototype.food = function (){
    return '大盘鸡'
}
function subType(age) {
    superType.call(this,age) // 借用父类的构造函数
}
subType.prototype = new superType()
subType.prototype.constructor = subType

let sub1 = new subType(25)
sub1.setname.nm = 'sub1的名字'
let sub2 = new subType(30)
console.log(sub1) // 输出：{ age: 25, setname: { nm: 'sub1的名字' } }
console.log(sub2) // 输出：{ age: 30, setname: { nm: '父类的名字' } }
console.log(sub2.food()) // 输出：大盘鸡


/*=======================================================================*/
/*
    寄生组合继承
*/

function superType(age) {
    this.setname={nm: '父类的名字'}
    this.age = age
}
superType.prototype.food = function () {
    return '大盘鸡'
}
function subType(age) {
    superType.call(this,age)
}
subType.prototype = Object.create(superType.prototype)
subType.prototype.constructor = subType

let sub1 = new subType(25)
sub1.setname.nm = 'sub1的名字'
let sub2 = new subType(31)
console.log(sub1)
console.log(sub2)
console.log(sub2.food())

/*=======================================================================*/
/**
 * ES6 继承
 */
class superType{
    constructor(age){
        this.age = age
        this.setname = {nm: '父类的名字'}
    }
    food(){
        return '大盘鸡'
    }
}
class subType extends superType{
    constructor(age){
        super(age)
    }
}

let sub1 = new subType(26)
sub1.setname.nm = 'sub1的名字'
let sub2 = new subType(30)
console.log(sub1) // 输出：subType { age: 26, setname: { nm: 'sub1的名字' } }
console.log(sub2) // 输出: { age: 30, setname: { nm: '父类的名字' } }
console.log(sub2.food()) // 输出：大盘鸡